package com.xie.o2o.config.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

/**
 * 对应spring-service里的transactionManger
 * 实现TransactionManagementConfigurer是因为开启annotation-driven
 * @create: xie
 */
@Configuration
public class TransactionManageMentConfiguration implements TransactionManagementConfigurer {

    //注入DataSourceConfiguration里的dataSource,通过createDataSource()获取
    @Autowired
    private DataSource dataSource;

    /**
     * 关于事务管理，需要返回PlatformTransactionManager的实现
     */
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }
}
